package com.cloud.base.thread.lock;

import lombok.SneakyThrows;

import java.util.concurrent.locks.ReentrantLock;

public class LockMain {
    public static void main(String[] args) {
        ReentrantLock reentrantLock = new ReentrantLock();
        new Thread(() -> {
            del(reentrantLock);
        }).start();
        new Thread(() -> {
            del(reentrantLock);
        }).start();
    }

    @SneakyThrows
    public static void del(ReentrantLock reentrantLock) {
        try {
            reentrantLock.lock();
            System.out.println("del执行开始:" + Thread.currentThread().getName());
            Thread.sleep(5000);
            System.out.println("del执行结束:" + Thread.currentThread().getName());
        } catch (Exception e) {
        } finally {
            reentrantLock.unlock();
        }
    }
}
